$(function(){
    $.ajax({
        url:wxUrl.searchOrderById,
        type:"post",
        dataType:"json",
        contentType:"application/json",
        beforeSend:function (request) {
            request.setRequestHeader("token",localStorage.getItem("token"));
        },
        data:JSON.stringify({
            "orderId":2
        }),
        success:function (resp) {
            let order = resp.order
            let status = order.status
            if(status==1){
                status = "未付款"
            }else if(status==2){
                status = "已付款"
            }else if(status==3){
                status = "已发货"
            }else if(status==4){
                status = "已签收"
            }
            //订单的金额
            let amount = order.amount
            //订单的创建日期
            let date = order.createTime
            //订单的流水号
            let code = order.code

            $("#code").text(code)
            $("#status").text(status)
            $("#date").text(date)
            $("#amount").text(amount)
            $("#pay").text(amount)

            $(".pay-btn").attr("data-order-id",order.id)

            if(status=="已付款"){
                $(".pay-btn").attr("disabled",true)
                $(".pay-btn").css({
                    "background-color":"#666"
                })
            }

            /**
             * 结算支付按钮点击事件
             */
            $(".pay-btn").click(function(){
                //点击之后按钮就变成禁用状态
                $(this).attr("disabled",true)
                $(".message").text("扫码中，请不要做其他操作")
                let authCode = ""
                //绑定键盘点击事件
                $(document).bind("keydown",function(event){
                    if(event.keyCode==13){
                        /** 用户按下了回车键 */

                        console.log(authCode)
                        let orderId = $(".pay-btn").data("order-id");
                        /** 用正则表达式判断是支付宝付款码还是微信付款码 **/
                        let url = ""
                        if(/^1[0-5][0-9]{16}&/.test(authCode)){
                            //微信付款码
                            url = wxUrl.scanCodePayOrder

                        }else if(/^(25|26|27|28|29|30)[0-9]{14,22}$/.test(authCode)){
                            //支付宝付款码
                            url = zfbUrl.scanCodePayOrder

                        }else if(/^622[0-9]{16}$/.test(authCode)){
                            //银联付款码
                            url = unionUrl.scanCodePayOrder

                        }

                        $.ajax({
                            url:url,
                            type:"post",
                            dataType: "json",
                            contentType: "application/json",
                            beforeSend:function (request) {
                                request.setRequestHeader("token",localStorage.getItem("token"));
                            },
                            data:JSON.stringify({
                                "orderId":orderId,
                                "authCode":authCode
                            }),
                            success:function(resp){
                                let msg = resp.msg
                                $(".message").text(msg)

                                if(msg == "付款成功"){
                                    //取消键盘输入事件
                                    $(document).unbind("keydown")
                                    setTimeout(function(){
                                        location.reload()
                                    },2000)
                                }
                            }
                        })

                    }else if(event.key==27){
                        /** 用户按下了Esc */
                        $(document).unbind("keydown")
                        $(".message").text("支付设备已经准备好")
                        $(".pay-btn").removeAttr("disabled")
                    }
                    //将输入字符累加到authCode
                    let ch = String.fromCharCode(event.keyCode)
                    authCode += ch
                })
            })
        }
    })
})